﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Dune2013
{
    class ActorManager
    {
        public List<Actor> actors = new List<Actor>();
        public static ActorManager instance;
        public static List<Actor> aBorrar = new List<Actor>();
        public static ActorManager getInstance() {
            if (instance == null) instance = new ActorManager();
            return instance;
        }

        public void update(int dTime){
            vaciarLista();
            foreach (Actor actor in actors) {
                actor.update(dTime);
            }
        }
        public void draw(SpriteBatch sb)
        {
            foreach (Actor actor in actors)
            {
                actor.draw(sb);
            }
        }
        public Actor findClosest(Actor targetAct, int type)
        {
            float minDistance=float.MaxValue;
            Actor closestActor = null;
            foreach (Actor act in actors)
            {
                if (targetAct != act && act.type == type && (targetAct.position - act.position).Length() < minDistance)
                {
                    closestActor = act;
                    minDistance = (targetAct.position - act.position).Length();
                }
            }
            return closestActor;
        }

        public void borrar(Actor act) {
            aBorrar.Add(act);
        }

        private void vaciarLista() {
            foreach (Actor actor in aBorrar)
            {
                actors.Remove(actor);
            }
            aBorrar.Clear();
        }
        
    }
}
